#include <stdlib.h>
#include <dlfcn.h>
#include <android_native_app_glue.h>

static char log_file_name[150];

// 用于输出日志到文件
static void log_cat(const char *msg) {
    FILE* fp = fopen(log_file_name, "a");
    if (!fp) {
        return;
    }
    fprintf(fp, "%s\n", msg);
    fclose(fp);
}

typedef void (*MainFunc)(struct android_app*);
static const char* exPath;
static const char* inPath;

void android_main(struct android_app* app) {
    exPath = app->activity->externalDataPath;
    inPath = app->activity->internalDataPath;
    sprintf(log_file_name, "%s/starter.log", exPath);

    char command[2000];
    sprintf(command, "cp %s/libmain.so %s", exPath, inPath);
    system(command);
    log_cat("command is :");
    log_cat(command);
    log_cat("");

    char* msg = NULL;
    char lib_path[150];
    // 指定库的路径并打开
    sprintf(lib_path, "%s/libmain.so", inPath);
    void* lib = dlopen(lib_path, RTLD_NOW);
    if ((msg=dlerror(),msg)) {
        log_cat(msg);
        return;
    }
    // 解析函数并运行
    MainFunc f = (MainFunc)dlsym(lib, "android_main");
    if ((msg=dlerror(),msg)) {
        log_cat(msg);
        return;
    }
    f(app);
    dlclose(lib);
}